﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web;

namespace LevenBlog.Core.User
{
    [Serializable]
    public class Authority : IAuthority
    {
        //前台权限
        public bool Post { get; set; }

        public bool Edit { get; set; }

        public bool Delete { get; set; }

        public bool Comment { get; set; }

        public bool CommentNeedVerify { get; set; }

        public bool Track { get; set; }

        public bool PostHtml { get; set; }

        public bool Upload { get; set; }

        public int UploadSize { get; set; }

        public int CommentLength { get; set; }

        //后台权限
        public bool AdminLink { get; set; }

        public bool Config { get; set; }

        public bool Category { get; set; }

        public bool ManageComment { get; set; }

        public bool ManageTrack { get; set; }

        public bool File { get; set; }

        public bool Image { get; set; }

        public bool Log { get; set; }

        public bool User { get; set; }

        public bool UserType { get; set; }

        public bool Super { get; set; }

        public bool Visiter { get; set; }

        public Authority()
        {
            this.AdminLink = false;
            this.Category = false;
            this.Comment = true;
            this.Config = false;
            this.Delete = false;
            this.Edit = false;
            this.File = false;
            this.Image = false;
            this.Log = false;
            this.ManageComment = false;
            this.ManageTrack = false;
            this.Post = false;
            this.PostHtml = false;
            this.Super = false;
            this.Track = false;
            this.Upload = false;
            this.User = false;
            this.UserType = false;
            this.Visiter = false;
            this.UploadSize = 1024;
            this.CommentLength = 500;
            this.CommentNeedVerify = false;
        }

        private static PropertyInfo[] properties = null;

        protected virtual PropertyInfo[] GetProperties()
        {
            if (properties == null)
                properties = this.GetType().GetProperties();
            return properties;
        }

        public virtual string ToCode()
        {
            StringBuilder sb = new StringBuilder();
            foreach (PropertyInfo pi in GetProperties())
            {
                sb.AppendFormat("{0}={1}|", pi.Name, pi.GetValue(this, null));
            }
            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }

        public virtual void Fill(string code)
        {
            string[] ss = code.Split('|');
            foreach (string s in ss)
            {
                string[] tags = s.Split('=');
                if (tags.Length != 2)
                    throw new Exception("错误的Code信息");
                PropertyInfo p = GetProperties().Single(gp => gp.Name == tags[0]);
                p.SetValue(this, Convert.ChangeType(tags[1], p.PropertyType), null);
            }
        }

        public virtual void FillWithWebRequest(System.Web.HttpRequestBase request)
        {
            PropertyInfo[] pis = GetProperties();
            foreach (PropertyInfo p in pis)
            {
                string temp = request.Form[p.Name];
                if (!string.IsNullOrEmpty(temp))
                {
                    p.SetValue(this, Convert.ChangeType(temp, p.PropertyType), null);
                }
                else
                {
                    p.SetValue(this, false, null);
                }
            }
        }
    }
}
